<template>
	<view>
		<!-- 搜索组件 -->
		<my-search @click="gotoSearch"></my-search>
		<view class="cate-list-container">
			<scroll-view class="cate-list-left" scroll-y="true" :style="{height:high + 'px'}">
				<block v-for="(item,index) in cateList" :key="index">
					<view :class="['cate-list-left-item',index == active ? 'active' :'']" @click="activeChange(index)">{{item.cat_name}}</view>
				</block>
			</scroll-view>
			<scroll-view class="cate-list-right" :style="{height:high + 'px'}"  scroll-y="true" :scroll-top="scrollTop">
				<view class="cate-list-right-item" v-for="(item,index) in cateLevel2" :key="index">
					<view class="cate-level2-title">
						/{{item.cat_name}}/
					</view>
					<!-- 三级分类 -->
					<view class="cate-level3">
						<!-- 三级分类的item项 -->
						<view class="cate-level3-item" v-for="(item,index) in cateLevel2[index].children" :key="index" @click="gotoGoodsList(item)">
							<image :src="item.cat_icon" mode=""></image>
							<!-- 三级分类的文本 -->
							<text>{{item.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixin/tabbar-badge.js'
	export default {
		mixins:[badgeMix],
		data() {
			return {
				// 可视化窗口的高度
				high:0,
				// 默认激活项
				active:0,
				// 分类数据
				cateList:[],
				//二级分类
				cateLevel2:[],
				// 滚动条距离上部的距离
				scrollTop:0
			};
		},
		onLoad() {
		const syncinfo = uni.getSystemInfoSync();
		// console.log(syncinfo);
		this.high = syncinfo.windowHeight - 45;
		this.getCateList()
		},
		methods:{
			async getCateList(){
				const {data:res} = await uni.$http.get('api/public/v1/categories');
				if(res.meta.status !== 200){
					return uni.$showMsg();
				}
				this.cateList = res.message;
				// console.log(this.cateList);
				this.cateLevel2 = res.message[0].children;
				// console.log(this.cateLevel2)
			},
			//点击左侧item项时激活项发生改变
			activeChange(index){
				this.active = index;
				// 重新为二级分类赋值	
				this.cateLevel2 = this.cateList[index].children;
				this.scrollTop = this.scrollTop == 0? 1: 0
			},
			// 点击三级分类跳转到good_list界面
			gotoGoodsList(item){
				uni.navigateTo({
					url:"../../subpkg/goods_list/goods_list?cid=" + item.cat_id
				})
			},
			// 点击跳转到搜索页面
			gotoSearch(){
				uni.navigateTo({
					url:"../../subpkg/search/search"
				})
			}
		},
		
	}
</script>

<style lang="scss">
.cate-list-container{
	display: flex;
	.cate-list-left{
		width: 100px;
	}
	.cate-list-left-item{
		width: 100px;
		line-height: 60px;
		background-color: #F7F7F7;
		text-align: center;
		font-size: 12px;
		&.active{
			background-color: #fff;
			position: relative;
			&::before{
				content: "";
				width: 3px;
				height: 30px;
				background-color:#c00000;
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
			}
		}
	}
}
.cate-list-right{
	background-color: #fff;
}
.cate-level2-title{
	font-size: 12px;
	font-weight: bold;
	text-align: center;	
	padding: 15px 0;
}
.cate-level3{
	display: flex;
	align-items: center;
	flex-wrap: wrap;
	.cate-level3-item{
		width: 33.33%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		margin: 10px 0;
		image{
			width: 60px;
			height: 60px;
		}
		text{
			font-size: 12px;
			padding-top: 5px;
		}
	}
}
</style>
